<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="线索名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['leadsName', validatorRules.leadsName]"
                placeholder="请输入线索名称"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="线索来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['source', validatorRules.source]"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryId, dictionaryTypeId =CRM_SOURCE_TYPE"
                placeholder="请选择线索来源"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="手机" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['mobile', validatorRules.mobile]"
                placeholder="请输入手机"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['telephone']"
                placeholder="请输入电话"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['email']" placeholder="请输入邮箱"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['address']" placeholder="请输入地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="客户行业" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['industry']"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryId, dictionaryTypeId =CRM_TRADE_TYPE"
                placeholder="请选择客户行业"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="客户级别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['level']"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryId, dictionaryTypeId =LEVEL_TYPE"
                placeholder="请选择客户级别"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="下次联系时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请输入下次联系时间"
                :show-time="true"
                v-decorator="['nextTime']"
                :trigger-change="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea :disabled="disableSubmit" v-decorator="['remark']" rows="4" placeholder="请输入备注" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JDate from '@/components/jeecg/JDate'
import validate from '@/utils/validate'

export default {
  name: 'CrmLeadsModal',
  components: {
    JDate,
    JDictSelectTag,
  },
  data() {
    return {
      disableSubmit: false,
      form: this.$form.createForm(this),
      title: '操作',
      width: 1000,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        leadsName: {
          rules: [{ required: true, message: '请输入线索名称!' }],
        },
        source: {
          rules: [{ required: true, message: '请选择线索来源!' }],
        },
        mobile: {
          rules: [
            { pattern: /^1[0-9]{10}$/, message: '请输入正确的手机号!' },
          ],
        },
        telephone: {
          rules: [
            { pattern: /^([0-9]{3,4}-)?[0-9]{7,8}$/, message: '请输入正确的电话号码!' },
          ],
        },
        email: {
          rules: [
            { pattern: /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/, message: '请输入正确邮箱!' },
          ],
        },
      },
      url: {
        add: '/online/api/createCrmLeads',
        edit: '/online/api/updateCrmLeads',
        get: '/online/api/getCrmLeads',
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.visible = true
      this.model = {}
      if (record.id) {
        httpAction(this.url.get, { id: record.id }, 'post').then((res) => {
          if (res.success) {
            this.model = Object.assign({}, res.result)
            this.$nextTick(() => {
              this.form.setFieldsValue(
                pick(
                  this.model,
                  'isTransform',
                  'followUp',
                  'leadsName',
                  'customerId',
                  'nextTime',
                  'telephone',
                  'mobile',
                  'email',
                  'address',
                  'remark',
                  'createUserId',
                  'ownerUserId',
                  'createTime',
                  'updateTime',
                  'batchId',
                  'isReceive',
                  'lastTime',
                  'lastContent',
                  'source',
                  'industry',
                  'level'
                )
              )
            })
          }
        })
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                if (that.model.id) {
                  that.$emit('loadDetailInfo', that.model)
                }
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'isTransform',
          'followUp',
          'leadsName',
          'customerId',
          'nextTime',
          'telephone',
          'mobile',
          'email',
          'address',
          'remark',
          'createUserId',
          'ownerUserId',
          'createTime',
          'updateTime',
          'batchId',
          'isReceive',
          'lastTime',
          'lastContent',
          'source',
          'industry',
          'level'
        )
      )
    },
  },
}
</script>


<style lang="less" scoped>
/deep/ .ant-modal-mask {
  z-index: 98;
  background-color: rgb(245, 246, 249);
}
/deep/ .ant-modal-wrap {
  z-index: 99;
}
/deep/ .ant-modal-title {
  color: #333;
  font-weight: 700;
  font-size: 17px;
}
</style>